use neutrino::{Window};
use neutrino::widgets::label::Label;
use neutrino::widgets::textinput::TextInput;
use neutrino::widgets::widget::Widget;
use neutrino::widgets::button::Button;
use neutrino::widgets::container::{Container,Direction};

trait ContainerExtend {
    fn add(&mut self,  widget: Box<dyn Widget>);
    fn set_direction(&mut self, directiton: Direction);
}

impl ContainerExtend for Window {
    fn add(&mut self, widget: Box<dyn Widget>) {
        self.set_child(widget);
    }

    fn set_direction(&mut self, _: Direction){

    }
}


#[macro_export]
macro_rules! with_widowns {
    
    (@win [ $($last:tt),* ]) => {
        {
            let mut layer = Container::new("single_layer");
            with_container!();
        }
    };
    (@win $last :expr) => {
        {
            let raw = $last;
            
            let mut layer = Container::new("single_layer");
            let (a,b) = raw;
            layer.add(Box::new(a));
            layer.set_direction(b);
    
            // let (widget, di) = raw;
            // let mut layer = Container::new("single_layer");

            layer
        }
    };
    (@win $win:expr, $_last:expr) =>{
        {

            let mut main = with_widowns!(@win $win);
            let sub = with_widowns!(@win $_last);
            &main.add(Box::new(sub));
            main
        }
    };
    
    (@win $win:expr , $($rest:tt),* ) => {
        {
            let sub = with_widowns!(@win $($rest),* );
            let mut main = with_widowns!(@win $win);
            main.add(Box::new(sub));
            main
        }
    };
    ( ($w:expr, $h:expr) , $($rest:tt),+ ) => {
        {
            let mut win = Window::new();
            win.set_size($w, $h);
            let sub = with_widowns!(@win $($rest),+);
            // println!("{}",stringify!($($tt),*));
            win.set_child(Box::new(sub));
            win
            
        }
    }
}


// #[macro_export]
// macro_rules! with_elements {
//     ((C ))  => {
//         {
//             let mut c = Container::new("con");
//             c.set_direction(Direction::Vertical);
//             c
//         }
//     };
//     ((L $($content:tt)*) )  => {
//         {
//             use neutrino::widgets::textinput::TextInput;
            
//             let mut t = Label::new("input");
//             let name = stringify!($($content)*);
//             t.set_text( name);
//             // t.set_text(concat!($(stringify!($content)),*));
//             t
//         }
//     };
    
//     ( (I  $content:tt ) ) => {
//         {
//             let t = TextInput::new("input");
//             // t.set_text(concat!($(stringify!($content)),*));
//             // t.set_listener(Box::new(MyTextInputListener));
//             t.set_style($content);
//             t
//         }
//     };
//     ((B $($content:tt)*)) => {
//         {
//             let mut t = Button::new("input");
//             t.set_text(stringify!($($content)*));
//             t
//         }
//     };
//     ( $( ( $($i:tt)* )   ),* ) =>{
//         {
//             let mut con = with_elements!((C ));
//             $(
//                 let e = with_elements!(( $($i:tt)* ) );
//                 con.add(Box::new(e));
//             )*
//             con
//         }
//     }
// }

// pub fn window() -> Window {
//     let mut win = Window::new();
//     win.set_size(320, 240);
//     // window.set_child(Box::new(label));
//     let mut win = with_widowns!( (320, 100) ,{
//         let label = Label::new("search in file.");
//         (label, Direction::Vertical)
//     },{
//         let input = TextInput::new("first !");
//         (input,Direction::Vertical)
//     },{
//         (Label::new("here !!!"), Direction::Vertical)
//     });
//     win.set_title("Hello World");
    

//     let cc = with_elements!(
//         (T sdf asdf),
//         (L hello world ! sdf),
//         (B to do)
//     );
//     win.add(Box::new(cc));
//     win
// }